package com.example.goodpet.chat.fragment;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.example.goodpet.R;
import com.example.goodpet.chat.activity.ChatActivity;
import com.example.goodpet.chat.activity.ChatSearchActivity;
import com.example.goodpet.chat.broadcastreceiver.MessageReceivedReceiver;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.modules.conversation.EaseConversationListFragment;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.easeui.widget.EaseSearchTextView;

public class MyEaseConversationListFragment extends EaseConversationListFragment implements View.OnClickListener {
    private EaseSearchTextView tvSearch;

    private MessageReceivedReceiver messageReceivedReceiver;

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        //添加搜索会话布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_search, null);
        llRoot.addView(view, 0);
        tvSearch = view.findViewById(R.id.tv_search);
        //conversationListLayout.getListAdapter().setEmptyLayoutId(R.layout.ease_layout_default_no_data);

        messageReceivedReceiver=new MessageReceivedReceiver(this);
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(MessageReceivedReceiver.BROAD_CAST_MSG_RECEIVED);
        getContext().registerReceiver(messageReceivedReceiver,intentFilter);
    }

    @Override
    public void initListener() {
        super.initListener();
        tvSearch.setOnClickListener(this);
    }
    @Override
    public void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        Object item = conversationListLayout.getItem(position).getInfo();
        if(item instanceof EMConversation) {
                ChatActivity.actionStart(mContext, ((EMConversation)item).conversationId(), EaseCommonUtils.getChatType((EMConversation) item));
            }
        }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_search :
                startActivity(new Intent(getActivity(), ChatSearchActivity.class));
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getContext().unregisterReceiver(messageReceivedReceiver);
    }
}
